package com.zeildlin.seecode.Controller;

import com.zeildlin.seecode.Service.CommentService;
import com.zeildlin.seecode.Service.DiscussPostService;
import com.zeildlin.seecode.Service.UserService;
import com.zeildlin.seecode.entity.Comment;
import com.zeildlin.seecode.entity.DiscussPost;
import com.zeildlin.seecode.entity.Page;
import com.zeildlin.seecode.entity.User;
import com.zeildlin.seecode.util.CommunitConstant;
import com.zeildlin.seecode.util.CommunityUtil;
import com.zeildlin.seecode.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunitConstant {

    @Autowired
    DiscussPostService discussPostService;
    @Autowired
    HostHolder hostHolder;
    @Autowired
    UserService userService;
    @Autowired
    CommentService commentService;
    @PostMapping(path = "/add")
    @ResponseBody
    public String addDiscussPost(String title,String content){
        User user = hostHolder.getUser();
        if(user ==null){
            return CommunityUtil.getJSONString(403,"你还没有登陆");
        }
        DiscussPost post = new DiscussPost();
        post.setUserId(user.getId());
        post.setContent(content);
        post.setTitle(title);
        post.setCreateTime(new Date());
        discussPostService.addDiscussPost(post);

        return CommunityUtil.getJSONString(0,"发布成功");
    }

    @GetMapping("/detail/{discussPostId}")
    public String getDiscussPost(@PathVariable("discussPostId")int discussPostId,Model model,Page page){
        //帖子
        DiscussPost discussPost = discussPostService.findDiscussPostById(discussPostId);
        //作者
        User user = userService.FindUserById(discussPost.getUserId());

        //评论分页信息
        page.setLimit(5);
        page.setPath("/discuss/detail"+discussPostId);
        page.setRows(discussPost.getCommentCount());

        //评论：给帖子的评论
        //回复：给评论的回复
        //评论列表
        List<Comment> commentList =
                commentService.findCommentsByEntity(ENTITY_TYPE_POST,discussPost.getId(),page.getOffset(),page.getLimit());
        //评论显示列表平
        List<Map<String,Object>> commentVoList = new ArrayList<>();
        if(commentList != null){
            for(Comment comment:commentList){
                Map<String,Object> commentVo = new HashMap<>();
                //评论
                commentVo.put("comment",comment);
                //评论作者
                commentVo.put("user", userService.FindUserById(comment.getUserId()));

                //回复列表
                List<Comment> replyList = commentService
                        .findCommentsByEntity(ENTITY_TYPE_COMMENT,comment.getId(),0,Integer.MAX_VALUE);
                //回复Vo列表
                List<Map<String,Object>> replyVoList = new ArrayList<>();
                if(replyList != null){
                    for(Comment reply: replyList){
                        Map<String,Object> replyVo = new HashMap<>();
                        //回复
                        replyVo.put("reply",reply);
                        //作者
                        replyVo.put("user",userService.FindUserById(reply.getUserId()));
                        //回复的目标
                        User target = reply.getTargetId() == 0?null:userService.FindUserById(reply.getTargetId());
                        replyVo.put("target",target);
                        replyVoList.add(replyVo);
                    }
                }
                commentVo.put("replys",replyVoList);
                //回复数量
                int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT,comment.getId());
                commentVo.put("replyCount",replyCount);
                commentVoList.add(commentVo);
            }
        }
        model.addAttribute("comments",commentVoList);
        model.addAttribute("post",discussPost);
        model.addAttribute("user",user);

        return "/site/discuss-detail";
    }

}
